<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="工号">
          <a-input placeholder="请输入" v-decorator="['StaffNo', validatorRules.StaffNo]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="姓名">
          <a-input placeholder="请输入姓名" v-decorator="['Name', validatorRules.name]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="性别">
          <a-select :disabled="editType=='detail'" placeholder="请输入性别" v-decorator="['GenderId', validatorRules.genderid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option value="3">男性</a-select-option>
            <a-select-option value="4">女性</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="出生日期">
          <a-date-picker placeholder="请输入出生日期" v-decorator="['Birthday', validatorRules.birthday]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="手机">
          <a-input placeholder="请输入手机" v-decorator="['Tel', validatorRules.tel]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="入职日期">
          <a-date-picker placeholder="请输入入职日期" v-decorator="['JoinedDate', validatorRules.joineddate]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="职务">
          <a-input placeholder="请输入职务" v-decorator="['Position', validatorRules.position]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="在职状态">
          <a-select placeholder="请选择在职状态" v-decorator="['WorkStatusId', validatorRules.workstatusid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.BaseInfoId" v-for="(item,index) in workStatusData">{{item.Value}}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否User">
          <a-select placeholder="请选择是否User" v-decorator="['IsUser', validatorRules.isuser]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option value="1">是</a-select-option>
            <a-select-option value="0">否</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="用户角色">
          <a-select placeholder="请选择用户角色" v-decorator="['UserRoleId', validatorRules.UserRole]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.BaseInfoId" v-for="(item,index) in UserRoleData">{{item.Value}}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "AuthUserModal",
    data () {
      return {
        UserRoleData:[],
        workStatusData:[],
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        StaffNo:{rules: [{ required: true, message: '请输入工号!' }]},
        name:{rules: [{ required: true, message: '请输入姓名!' }]},
        password:{rules: [{ required: true, message: '请输入linker密码!' }]},
        genderid:{rules: [{ required: true, message: '请输入性别!' }]},
        birthday:{rules: [{ required: true, message: '请输入出生日期!' }]},
        highdegree:{rules: [{ required: true, message: '请输入最高学历!' }]},
        school:{rules: [{ required: true, message: '请输入毕业学校!' }]},
        adress:{rules: [{ required: true, message: '请输入现居住地!' }]},
        tel:{rules: [{ required: true, message: '请输入手机!' }]},
        wechat:{rules: [{ required: true, message: '请输入微信!' }]},
        joineddate:{rules: [{ required: true, message: '请输入入职日期!' }]},
        position:{rules: [{ required: true, message: '请输入职务!' }]},
        workstatusid:{rules: [{ required: true, message: '请输入在职状态!' }]},
        provinceid:{rules: [{ required: true, message: '请输入籍贯省!' }]},
        cityid:{rules: [{ required: true, message: '请输入籍贯市!' }]},
        districtid:{rules: [{ required: true, message: '请输入籍贯区/县!' }]},
        marriagebearing:{rules: [{ required: true, message: '请输入婚育情况!' }]},
        emergencycontact:{rules: [{ required: true, message: '请输入紧急联系人!' }]},
        contacttel:{rules: [{ required: true, message: '请输入紧急联系人手机!' }]},
        UserRole:{rules: [{ required: true, message: '请选择用户角色!' }]},
        isuser:{rules: [{ required: true, message: '请选择是否User!' }]},
        ts:{rules: [{ required: true, message: '请输入最近更新日期!' }]},
        datastatus:{rules: [{ required: true, message: '请输入状态!' }]},
        gender:{rules: [{ required: true, message: '请输入!' }]},
        updatebyid:{rules: [{ required: true, message: '请输入最近修改人!' }]},
        cellphone:{rules: [{ required: true, message: '请输入!' }]},
        ct:{rules: [{ required: true, message: '请输入!' }]},
        },
        url: {
          add: "/user/add",
          edit: "/user/update",
          baseList:"/baseInfo/queryRows",
        },
      }
    },
    created () {
      this.getBaseData();
    },
    methods: {
      getBaseData(){
        this.$http.postAction(this.url.baseList,{pageNo:1,pageSize:100,selectFields:[],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'WorkStatus'
            }]}).then((res)=>{
          if(res.status == 1){
            this.workStatusData = res.data.rows;
          }
        })
        this.$http.postAction(this.url.baseList,{pageNo:1,pageSize:100,selectFields:[],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'UserRole'
            }]}).then((res)=>{
          if(res.status == 1){
            this.UserRoleData = res.data.rows;
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.UserRoleId = this.model.UserRole;
        this.model.GenderId = this.model.Gender;
        this.model.WorkStatusId = this.model.WorkStatus;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'Name','StaffNo','GenderId','Tel','Position','WorkStatusId','IsUser','UserRoleId'))
		  //时间格式化
      this.form.setFieldsValue({Birthday:this.model.Birthday?moment(this.model.Birthday,'YYYY-MM-DD'):null})
      this.form.setFieldsValue({JoinedDate:this.model.JoinedDate?moment(this.model.JoinedDate,'YYYY-MM-DD'):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.Id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.Birthday = formData.Birthday?formData.Birthday.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.JoinedDate = formData.JoinedDate?formData.JoinedDate.format('YYYY-MM-DD HH:mm:ss'):null;
            console.log(formData)
            this.$http.postAction(httpurl,formData,method).then((res)=>{
              if(res.status == 1){
                that.$message.success(res.message);
                if(method == 'post'){
                  this.form.resetFields();
                  this.model = {};
                }else{
                  that.close();
                }
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>